package com.prs.crm.action.document;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.document.DraftFlow;
import com.prs.crm.domain.document.DraftFlowType;
import com.prs.crm.domain.document.DraftFormworkType;
import com.prs.crm.domain.document.DraftType;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.document.DraftFlowService;
import com.prs.crm.service.document.DraftFlowTypeService;
import com.prs.crm.service.document.DraftFormworkTypeService;
import com.prs.crm.service.document.DraftTypeService;

public class DraftTypeAction extends BaseAction{

	private static final long serialVersionUID = 1L;
		private DraftType draftType;
		private DraftTypeService draftTypeService;
		private PaginationSupport pagination;
		private Integer[] ids;
		private List<DraftFormworkType> draftFormworkType;
		private DraftFormworkTypeService draftFormworkTypeService;
		
		private List<DraftFlow> draftFlow;
		private DraftFlowService draftFlowService;
		
		private List<DraftFlowType> draftFlowType;
		private DraftFlowTypeService draftFlowTypeService;
		
		private List<DraftType> draftTypes;
		
		public List<DraftType> getDraftTypes() {
			return draftTypes;
		}
		public void setDraftTypes(List<DraftType> draftTypes) {
			this.draftTypes = draftTypes;
		}
		public List<DraftFlowType> getDraftFlowType() {
			return draftFlowType;
		}
		public void setDraftFlowType(List<DraftFlowType> draftFlowType) {
			this.draftFlowType = draftFlowType;
		}
		public DraftFlowTypeService getDraftFlowTypeService() {
			return draftFlowTypeService;
		}
		public void setDraftFlowTypeService(DraftFlowTypeService draftFlowTypeService) {
			this.draftFlowTypeService = draftFlowTypeService;
		}
		public List<DraftFlow> getDraftFlow() {
			return draftFlow;
		}
		public void setDraftFlow(List<DraftFlow> draftFlow) {
			this.draftFlow = draftFlow;
		}
		public DraftFlowService getDraftFlowService() {
			return draftFlowService;
		}
		public void setDraftFlowService(DraftFlowService draftFlowService) {
			this.draftFlowService = draftFlowService;
		}
		public List<DraftFormworkType> getDraftFormworkType() {
			return draftFormworkType;
		}
		public void setDraftFormworkType(List<DraftFormworkType> draftFormworkType) {
			this.draftFormworkType = draftFormworkType;
		}
		public DraftFormworkTypeService getDraftFormworkTypeService() {
			return draftFormworkTypeService;
		}
		public void setDraftFormworkTypeService(
				DraftFormworkTypeService draftFormworkTypeService) {
			this.draftFormworkTypeService = draftFormworkTypeService;
		}
		public DraftType getDraftType() {
			return draftType;
		}
		public void setDraftType(DraftType draftType) {
			this.draftType = draftType;
		}
		public DraftTypeService getDraftTypeService() {
			return draftTypeService;
		}
		public void setDraftTypeService(DraftTypeService draftTypeService) {
			this.draftTypeService = draftTypeService;
		}
		public PaginationSupport getPagination() {
			return pagination;
		}
		public void setPagination(PaginationSupport pagination) {
			this.pagination = pagination;
		}
		public Integer[] getIds() {
			return ids;
		}
		public void setIds(Integer[] ids) {
			this.ids = ids;
		}

		@Action("listDraftType")
		public String listDraftType(){
			Person per=AppContext.getAccountContext().currentUser();
			List<DraftType> dt=this.getDraftTypeService().getDraftTypes();
			for (DraftType dts : dt) {
				if (dts.getMoudlePerson()==null) {
					dts.setUpState("5");
					this.getDraftTypeService().submit(dts);
				}else{
					String[] wh=dts.getMoudlePerson().split(",");
					for (String whs : wh) {
						if (per.getResume().getName().equals(whs)) {
							dts.setUpState(per.getId()+":1");
							this.getDraftTypeService().submit(dts);
							break;
						}else{
							dts.setUpState("5");
							this.getDraftTypeService().submit(dts);
						}
					}
				}
			}
			DetachedCriteria criteria=this.getPaginationHelper().createQuery(DraftType.class);
			criteria.add(Restrictions.eq("upState", per.getId()+":1"));
			pagination=this.getPaginationHelper().prepare(pagination);
			this.setPagination(this.getDraftTypeService().listByPage(criteria, pagination));
			return SUCCESS;
		}
		@Action("editDraftType")
		public String editDraftType(){
			this.draftFormworkType=this.getDraftFormworkTypeService().getAllDraftFormworkType();
			return SUCCESS;
		}
		@Action("editFlow")
		public String editFlow(){
			this.draftFlow=this.getDraftFlowService().getAllDraftFLow();
			this.draftFlowType=this.getDraftFlowTypeService().getAllDraftFlowType();
				return SUCCESS;
		}
		@Action(value="saveDraftType",results={
				@Result(name="success",type="redirect",location="listDraftType"),
				@Result(name="error",type="chain",location="editDraftType")
		})
		public String saveDraftType(){
			if (draftType.getHoutai()==null ||"".equals(draftType.getHoutai().trim())) {
				this.addActionError("后台触发程序不能为空！");
				return ERROR;
			}
			if (draftType.getMoudlePerson()==null || "".equals(draftType.getMoudlePerson().trim()) || "0".equals(draftType.getMoudlePerson().trim())) {
				draftType.setMoudlePerson(AppContext.getAccountContext().currentUser().getResume().getName());
				draftType.setMoudlePersonId(AppContext.getAccountContext().currentUser().getId()+"");
			}
			this.getDraftTypeService().save(draftType);
			return SUCCESS;
		}
		@Action(value="removeDraftType",results={
				@Result(name="success",type="redirect",location="listDraftType"),
				@Result(name="error",type="chain",location="listDraftType")
		})
		public String removeDraftType(){
			if (draftType.getDraft().size()!=0) {
				this.addActionError("已有模板，不能删除！");
				return ERROR;
			}
			this.getDraftTypeService().remove(draftType);
			return SUCCESS;
		}
		
		@Action("draftModule")
		public String draftModule(){
			draftFormworkType=this.getDraftFormworkTypeService().getAllDraftFormworkType();
			draftTypes=this.getDraftTypeService().getAllDraftType();
			return SUCCESS;
		}
		@Action("viewDraftModule")
		public String viewDraftModule(){
			draftFormworkType=this.getDraftFormworkTypeService().getAllDraftFormworkType();
			draftTypes=this.getDraftTypeService().getAllDraftType();
			return SUCCESS;
		}
}
